package com.zzsy.carosshopprojects.activitys.home.mainpage.common.scan;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.invoice.ReceiptSufAc;
import com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.memberrecharge.phonerecharge.PhoneRechargeAc;
import com.zzsy.carosshopprojects.activitys.home.mainpage.common.ReadCardMode;
import com.zzsy.carosshopprojects.activitys.home.mainpage.common.ScanErCodeMode;
import com.zzsy.carosshopprojects.base.BaseActivity;
import com.zzsy.carosshopprojects.util.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 扫一扫 识别卡的扫一扫
 */
public class ScanAc extends BaseActivity {
    public static void start(Activity mactivity, int num) {
        Intent intent = new Intent(mactivity, ScanAc.class);
        intent.putExtra("num", num);
        mactivity.startActivity(intent);
        mactivity.overridePendingTransition(0, 0);
    }

    private int num;
    private ScanMode scanMode;
    private ReadCardMode readCardMode;
    private ScanErCodeMode scanErCodeMode;
    @BindView(R.id.tv_phone)
    TextView tvPhone;//手机号充值

    @Override
    protected int bindLayout() {
        return R.layout.ac_scan;
    }

    @Override
    protected void initView() {
        initTitle(getWindow().getDecorView(), getResources().getString(R.string.scan), this);
    }

    @Override
    protected void initData() {
        scanMode = getViewModelProvider().get(ScanMode.class);
        readCardMode = getViewModelProvider().get(ReadCardMode.class);
        getLifecycle().addObserver(readCardMode);
        scanErCodeMode = getViewModelProvider().get(ScanErCodeMode.class);
        num = getIntent().getIntExtra("num", 0);
        if (num == 3) {
            tvPhone.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void loadMethod() {
        checkCards();
        sanErCode();
        tokenResult();
        CodeResult();
    }

    @OnClick({R.id.tv_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_phone:
                PhoneRechargeAc.start(ScanAc.this);
                finish();
                break;
        }
    }


    /**
     * token 处理结果
     */
    private void tokenResult() {
        scanMode.getliveToken().observe(ScanAc.this, tokenBean -> {
            if (!StringUtils.isEmpty(tokenBean.getToken())) {
                scanMode.DrawBill(ScanAc.this, tokenBean.getResult(), tokenBean.getToken());
            } else {
                ToastUtils.showToast(getApplicationContext(), getApplicationContext().getResources().getString(R.string.dataError));
                finish();
            }
        });
    }

    /**
     * UUID查询会员信息结果
     *
     * @param uuid
     */
    private void getUUIDMemeberData(String uuid) {
        scanMode.MemberByUuid(this, uuid, num);
    }


    /**
     * 加载扫描控件
     */
    private void sanErCode() {
        scanErCodeMode.getLiveData().observe(this, result -> {
            if (num == 12) {
                if (result.indexOf("xcx_") != -1) {
                    getUUIDMemeberData(result);
                } else {
                    scanMode.getToken(ScanAc.this, result);
                }
            } else {
                getUUIDMemeberData(result);
            }

            // finish();
        });
        scanErCodeMode.sanErCode(ScanAc.this);

    }


    /**
     * 验卡
     */
    private void checkCards() {
        readCardMode.getLiveData().observe(this, uuid -> {
            if (uuid == null) {
                ToastUtils.showToast(getApplicationContext(), "获取卡片信息失败,请重新检测~");
                return;
            }
            getUUIDMemeberData(uuid);
        });
        readCardMode.checkCards(ScanAc.this);

    }

    private void CodeResult() {
        scanMode.getLiveCode().observe(this, integer -> {
            if (integer == 200) {
                finish();
                ReceiptSufAc.start(ScanAc.this);
            }
        });
    }
}
